package com.dev.demo;

import java.io.IOException;


public class MyClassLoader extends ClassLoader {

    private static final String BASE_DIR = "E:/idea-workspace/gitee/team-pay/target/classes/com/dev/demo";

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String fileName = name.replaceAll("\\.", "/");
        fileName = BASE_DIR + fileName + ".class";
        try {
            byte[] bytes = BinaryFileUtils.readFileToByteArray(fileName);
            return defineClass(name, bytes, 0, bytes.length);
        } catch (IOException ex) {
            throw new ClassNotFoundException("failed to load class " + name, ex);
        }
    }

    public static void main(String[] args) {
        String name = "com.dev.demo.IHelloServiceImpl";
        try {
            Class<?> class1 = new MyClassLoader().findClass(name);
            Class<?> class2 = new MyClassLoader().findClass(name);
            System.out.println(class1.equals(class2));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
